<template>
  <div class="header">
    <div class="w1200">
      <el-row class="wrapper">
        <el-col :span="4">
          <router-link to="/" class="logo"
            ><img src="../assets/logo.jpg" alt=""
          /></router-link>
        </el-col>
        <el-col :span="12">
          <ul class="nav">
            <li
              :class="[menuActive.indexOf(item.path) === 0 ? 'is-active' : '']"
              v-for="item in menuList"
              :key="item.path"
              @click="selectMenu(item.path)"
            >
              <span>{{ item.name }}</span>
            </li>
          </ul>
          <!-- <ul class="nav">
            <li class="is-active"><router-link class="page" to="/">首页</router-link></li>
            <li><router-link class="page" to="/rank">排行榜</router-link></li>
            <li><router-link class="page" to="/playlist">歌单</router-link></li>
            <li><router-link class="page" to="/mvlist">MV</router-link></li>
            <li><router-link class="page" to="/artist">歌手</router-link></li>
            <li><router-link class="page" to="/my">我的音乐</router-link></li>
          </ul> -->
        </el-col>
        <el-col :span="6" class="search-box">
          <span class="search">
            <el-popover
              width="200"
              placement="bottom-end"
              trigger="manual"
              v-model="isShowSearch"
              v-clickoutside="handleClose"
            >
              <el-input
                class="keyVal"
                slot="reference"
                placeholder="请输入歌名、歌词、歌手或专辑"
                v-model="keyVal"
                clearable
                @focus="handleFocus"
                @input="handleInput"
              ></el-input>
              <template>
                <div class="hot-search" v-if="!keyVal">
                  <h5>热门搜索</h5>
                  <div class="hot-search-list">
                    <div
                      class="hot-item"
                      v-for="(item, index) in serachHot"
                      :key="index"
                      @click="toSearch(item)"
                    >
                      <span :class="[index < 3 ? 'top-' + index : '']">{{
                        index + 1 + "."
                      }}</span>
                      {{ item.first }}
                    </div>
                  </div>
                </div>
                <div class="search-key-list" v-else>
                  <div
                    class="search-item"
                    v-for="(item, index) in suggestInfo.order"
                    :key="index"
                  >
                    <h6>{{ listType[item] }}</h6>
                    <div class="item-main">
                      <div
                        class="list"
                        v-for="(val, k) in suggestInfo[item]"
                        :key="k"
                        @click="toPage(val, item)"
                      >
                        {{ val.name }}
                        <template v-if="item === 'songs'">
                          -<span v-for="(a, i) in val.artists" :key="i">{{
                            a.name + (i !== 0 ? " / " : "")
                          }}</span>
                        </template>
                        <template v-else-if="item === 'albums'">
                          -<span>{{ val.artist.name }}</span>
                        </template>
                      </div>
                    </div>
                  </div>
                </div>
              </template>
            </el-popover>
            <i class="iconfont icon-search" @click="search"></i>
          </span>
        </el-col>
        <el-col :span="2" :class="isLogin ? 'user-avatar' : 'login'">
          <div class="logined" v-if="isLogin">
            <el-dropdown placement="bottom" @command="userMenuHandler">
              <el-image :src="userInfo.avatarUrl" class="avatar">
                <div slot="placeholder" class="image-slot">
                  <i class="iconfont icon-placeholder"></i>
                </div>
              </el-image>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="user"
                  ><router-link :to="{ name: 'user' }"
                    ><i class="iconfont icon-home"></i>我的主页</router-link
                  ></el-dropdown-item
                >
                <el-dropdown-item command="grade"
                  ><i class="iconfont icon-grade"></i>我的等级</el-dropdown-item
                >
                <el-dropdown-item command="set"
                  ><i class="iconfont icon-set"></i>设置</el-dropdown-item
                >
                <el-dropdown-item command="quit"
                  ><i class="iconfont icon-quit"></i>退出</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </div>
          <span class="login-btn" v-else @click="loginDialog">登录</span>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import { reqGetserachHot, reqGetserachSuggest } from "../api/home";
import { mapGetters, mapMutations, mapActions } from "vuex";
/**点击元素外面才会触发的事件 */
import Clickoutside from "element-ui/src/utils/clickoutside";
export default {
  name: "Header",
  directives: { Clickoutside },
  data() {
    return {
      menuList: [
        {
          name: "首页",
          path: "/index",
        },
        {
          name: "排行榜",
          path: "/rank",
        },
        {
          name: "歌单",
          path: "/playlist",
        },
        {
          name: "MV",
          path: "/mvlist",
        },
        {
          name: "歌手",
          path: "/artist",
        },
        {
          name: "我的音乐",
          path: "/my",
        },
      ],
      listType: {
        songs: "单曲",
        artists: "歌手",
        albums: "专辑",
        playlists: "歌单",
      },
      serachHot: [],
      suggestInfo: {},
      isShowSearch: false,
      keyVal: "",
      isshowlogin: false,
    };
  },
  computed: {
    ...mapGetters(["isLogin", "userInfo"]),
    ...mapActions(["loginOut"]),
    menuActive() {
      return this.$route.path;
    },
  },
  methods: {
    ...mapMutations(["setLoginDialog"]),
    // 热门搜索
    async getSearchHot() {
      const res = await reqGetserachHot();
      this.serachHot = res.result.hots;
    },
    // 通过关键字搜索
    async getSerachSuggest() {
      const keywords = this.keyVal;
      const res = await reqGetserachSuggest({ keywords });
      this.suggestInfo = res.result;
    },
    // 路由跳转
    selectMenu(item) {
      this.$router.push({
        path: item,
      });
    },
    // 默认搜索列表点击
    toSearch(item) {
      this.isShowSearch = false;
      this.keyVal = item.first;
      this.$router.push({ path: "/search", query: { key: item.first } });
    },
    // 根据关键字搜索跳转到指定页面
    toPage(item, type) {
      this.keyVal = item.name;
      switch (type) {
        case "songs":
          this.$router.push({ path: "/songs", query: { id: item.id } });
          break;
        case "artists":
          this.$router.push({ path: "/singer", query: { id: item.id } });
          break;
        case "albums":
          this.$router.push({ path: "/albums", query: { id: item.id } });
          break;
        case "playlists":
          this.$router.push({
            path: "/playlist/detail",
            query: { id: item.id },
          });
          break;
      }
    },
    showSearch() {
      // 显示搜索列表 若有关键词显示搜索的关键字，否则显示默认热门搜索列表
      if (!this.keyVal) {
        this.serachHot = [];
        this.getSearchHot();
      } else {
        this.suggestInfo = {};
        this.getSerachSuggest();
      }
    },
    search() {
      if (this.keyVal) {
        //点击搜索 跳转到搜索结果的界面
        this.$router.push({ path: "/search", query: { key: this.keyVal } });
        this.isShowSearch = false;
      }
    },
    handleFocus() {
      this.getSearchHot();
      this.isShowSearch = true;
    },
    handleClose() {
      this.isShowSearch = false;
    },
    handleInput() {
      if (this.keyVal) {
        this.showSearch();
        this.isShowSearch = true;
      } else {
        this.isShowSearch = false;
      }
    },
    // 顶部头像下拉菜单
    userMenuHandler(type) {
      switch (type) {
        case "home":
          console.log(111);
          break;
        case "grade":
          this.$router.push({
            name: "grade",
          });
          break;
        case "set":
          this.$router.push({
            name: "setting",
          });
          break;
        case "quit":
          console.log(this);
          this.loginOut();
          break;
      }
      console.log(type);
    },
    loginDialog() {
      this.setLoginDialog(true);
    },
  },
};
</script>

<style lang="less" scoped>
.header {
  position: fixed;
  top: 0;
  width: 100%;
  z-index: 90;
  font-size: 0;
  box-shadow: 0 2px 10px 0 rgba(52, 52, 52, 0.1);
  background: #fff;
}
.w1200 {
  width: 1200px;
  margin: 0px auto;
}
.wrapper {
  display: flex;
  align-items: center;
}
.logo {
  display: inline-block;
  height: 60px;
  line-height: 60px;
  padding: 10px 0;
}
.nav {
  border: 0;
  font-size: 0;

  li {
    display: inline-block;
    padding: 0;
    height: 80px;
    line-height: 80px;
    color: #909399;
    cursor: pointer;
    border-bottom: 2px solid transparent;
  }

  .is-active {
    border-bottom: 2px solid #303133;
    color: #303133;
  }

  span {
    padding: 0 20px;
    display: block;
    font-size: 14px;
    text-decoration: none;
  }
}
.search-box {
  margin-left: 200px;
}
.search {
  position: relative;
  display: block;
  text-align: right;

  .keyVal {
    width: 226px;
    line-height: 32px;
    border: 0;
    border-bottom: 1px solid #999;
    font-size: 14px;
    display: inline-block;

    /deep/input {
      -webkit-appearance: none;
      background-color: #fff;
      background-image: none;
      border-radius: 4px;
      border: 1px solid #dcdfe6;
      box-sizing: border-box;
      color: #606266;
      display: inline-block;
      font-size: inherit;
      height: 40px;
      line-height: 40px;
      outline: 0;
      padding: 0 15px;
      transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
      width: 100%;
      border: 0;
    }
  }

  .icon-search {
    font-size: 20px;
    margin: 10px 0px 5px 10px;
    position: absolute;
    cursor: pointer;
  }
}
.login {
  margin-left: 40px;
  text-align: center;

  .login-btn {
    font-size: 16px;
    cursor: pointer;
  }
}

  .hot-search {
    margin-top:-10px;
    background-color:#fff;
    box-shadow: 0 2px 10px 0 rgba(52, 52, 52, 0.1);
    padding: 10px;
    position: absolute;
    width: 205px;
    left: 0;
    z-index: 2001;
    h5 {
      font-size: 18px;
    }

  .hot-search-list {
    padding: 10px 0;
  }
  .hot-item {
    line-height: 28px;
    cursor: pointer;
    .top-0 {
      font-weight: bold;
      color: rgba(255, 0, 0, 1);
    }

    .top-1 {
      font-weight: bold;
      color: rgba(255, 0, 0, 0.6);
    }

    .top-2 {
      font-weight: bold;
      color: rgba(255, 0, 0, 0.4);
    }

    img {
      display: inline-block;
      width: auto;
      height: 16px;
      padding: 6px 0;
      vertical-align: top;
    }
  }
}
/*.search-key-list {*/
/*  margin-top: 15px;*/
/*  background-color: #fff;*/
/*  box-shadow: 0 2px 10px 0 rgba(52, 52, 52, 0.1);*/
/*  padding: 10px;*/
/*  position: relative;*/
/*  width: 220px;*/
/*  left: -30px;*/
/*  z-index: 50;*/
/*}*/

.search-key-list {
  margin-top:-10px;
  background-color:#fff;
  box-shadow: 0 2px 10px 0 rgba(52, 52, 52, 0.1);
  padding:10px;
  position:absolute;
  width:205px;
  left:0px;
  z-index:2221;
}

.search-item {
  border-top: solid 1px #f2f2f2;
  margin: -1px -12px 0;

  h6 {
    font-size: 14px;
    line-height: 36px;
    color: #666;
    padding: 0 12px;
  }

  .list {
    line-height: 36px;
    color: #999;
    padding: 0 12px 0 40px;
    display: block;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    cursor: pointer;

    &:hover {
      color: #fff;
      background: #ff641e;
    }
  }

  &:first-child {
    border: 0;
  }
}
.user-avatar {
  text-align: center;

  .avatar {
    display: inline-block;
    width: 40px;
    height: 40px;
    border-radius: 100%;
    overflow: hidden;
    cursor: pointer;
    margin-left: 40px;
  }

  .el-dropdown {
    padding: 20px 0;
    font-size: 0;
  }
}
.login {
  text-align: center;

  .login-btn {
    font-size: 16px;
    cursor: pointer;
  }
}
.el-dropdown-menu {
  display: flex;
  list-style: none;
  box-shadow: 0 2px 10px 0 rgba(52, 52, 52, 0.1);
  background: #fff;
  width: 150px;
  height: 150px;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
  margin-top: 15px;
}
</style>
